package cn.iocoder.yudao.module.fengdong.smartelderly.service.oldmanpointrecord;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.fengdong.smartelderly.controller.admin.oldmanpointrecord.vo.*;
import cn.iocoder.yudao.module.fengdong.smartelderly.dal.dataobject.oldmanpointrecord.OldmanPointRecordDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.fengdong.smartelderly.convert.oldmanpointrecord.OldmanPointRecordConvert;
import cn.iocoder.yudao.module.fengdong.smartelderly.dal.mysql.oldmanpointrecord.OldmanPointRecordMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.fengdong.smartelderly.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 老人积分记录 Service 实现类
 *
 * @author 沣东养老院管理员
 */
@Service
@Validated
public class OldmanPointRecordServiceImpl implements OldmanPointRecordService {

    @Resource
    private OldmanPointRecordMapper oldmanPointRecordMapper;

    @Override
    public Long createOldmanPointRecord(OMPointRecordCreateReqVO createReqVO) {
        // 插入
        OldmanPointRecordDO oldmanPointRecord = OldmanPointRecordConvert.INSTANCE.convert(createReqVO);
        oldmanPointRecordMapper.insert(oldmanPointRecord);
        // 返回
        return oldmanPointRecord.getId();
    }

    @Override
    public void updateOldmanPointRecord(OMPointRecordUpdateReqVO updateReqVO) {
        // 校验存在
        validateOldmanPointRecordExists(updateReqVO.getId());
        // 更新
        OldmanPointRecordDO updateObj = OldmanPointRecordConvert.INSTANCE.convert(updateReqVO);
        oldmanPointRecordMapper.updateById(updateObj);
    }

    @Override
    public void deleteOldmanPointRecord(Long id) {
        // 校验存在
        validateOldmanPointRecordExists(id);
        // 删除
        oldmanPointRecordMapper.deleteById(id);
    }

    private void validateOldmanPointRecordExists(Long id) {
        if (oldmanPointRecordMapper.selectById(id) == null) {
            throw exception(OLDMAN_POINT_RECORD_NOT_EXISTS);
        }
    }

    @Override
    public OldmanPointRecordDO getOldmanPointRecord(Long id) {
        return oldmanPointRecordMapper.selectById(id);
    }

    @Override
    public List<OldmanPointRecordDO> getOldmanPointRecordList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return oldmanPointRecordMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<OldmanPointRecordDO> getOldmanPointRecordPage(OMPointRecordPageReqVO pageReqVO) {
        return oldmanPointRecordMapper.selectPage(pageReqVO);
    }

    @Override
    public List<OldmanPointRecordDO> getOldmanPointRecordList(OMPointRecordExportReqVO exportReqVO) {
        return oldmanPointRecordMapper.selectList(exportReqVO);
    }

}
